package com.hb.podao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.core.BaseHibernateDAO;
import com.core.HqlParameter;

/**
 * Data access object (DAO) for domain model class HbReply.
 * 
 * @see com.hb.podao.HbReply
 * @author MyEclipse Persistence Tools
 */

public class HbReplyDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(HbReplyDAO.class);

	// property constants
	public static final String NEEDID = "needid";

	public static final String REPLIERID = "replierid";

	public static final String CONTENT = "content";

	public static final String LINKPHONE = "linkphone";

	public static final String LINKQQ = "linkqq";

	public static final String COSTFLAG = "costflag";

	public static final String REPLYSTATUS = "replystatus";

	public static final String BIDSTATUS = "bidstatus";

	public static final String FEETYPE = "feetype";

	public static final String COST = "cost";

	public static final String FEE = "fee";

	public void save(HbReply transientInstance) {
		log.debug("saving HbReply instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(HbReply persistentInstance) {
		log.debug("deleting HbReply instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public HbReply findById(java.lang.Integer id) {
		log.debug("getting HbReply instance with id: " + id);
		try {
			HbReply instance = (HbReply) getSession().get(
					"com.hb.podao.HbReply", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(HbReply instance) {
		log.debug("finding HbReply instance by example");
		try {
			List results = getSession().createCriteria("com.hb.podao.HbReply")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding HbReply instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from HbReply as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public  List findRecentReply(int size , Integer bidstatus, Integer replystatus, String needtype) {
		StringBuffer hql = new StringBuffer(" from HbReply t where 1 = 1");
		List args = new ArrayList();
		
		if (bidstatus != null) {
			hql.append(" and t.bidstatus = ?");
			args.add(new HqlParameter(bidstatus));
		}
		
		if (replystatus != null) {
			hql.append(" and t.replystatus = ?");
			args.add(new HqlParameter(replystatus));
		}
		
		if (needtype != null) {
			hql.append(" and t.need.needtype = ?");
			args.add(new HqlParameter(needtype));
		}
		
		hql.append(" order by t.replydate desc ");
		
		System.out.println(hql.toString());
		return super.findByHqlWithMaxResult(hql.toString(), args, size);
	}
	
	public List findByNeedid(Object needid) {
		return findByProperty(NEEDID, needid);
	}

	public List findByReplierid(Object replierid) {
		return findByProperty(REPLIERID, replierid);
	}

	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}

	public List findByLinkphone(Object linkphone) {
		return findByProperty(LINKPHONE, linkphone);
	}

	public List findByLinkqq(Object linkqq) {
		return findByProperty(LINKQQ, linkqq);
	}

	public List findByCostflag(Object costflag) {
		return findByProperty(COSTFLAG, costflag);
	}

	public List findByReplystatus(Object replystatus) {
		return findByProperty(REPLYSTATUS, replystatus);
	}

	public List findByBidstatus(Object bidstatus) {
		return findByProperty(BIDSTATUS, bidstatus);
	}

	public List findByFeetype(Object feetype) {
		return findByProperty(FEETYPE, feetype);
	}

	public List findByCost(Object cost) {
		return findByProperty(COST, cost);
	}

	public List findByFee(Object fee) {
		return findByProperty(FEE, fee);
	}

	public List findAll() {
		log.debug("finding all HbReply instances");
		try {
			String queryString = "from HbReply";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public HbReply merge(HbReply detachedInstance) {
		log.debug("merging HbReply instance");
		try {
			HbReply result = (HbReply) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(HbReply instance) {
		log.debug("attaching dirty HbReply instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(HbReply instance) {
		log.debug("attaching clean HbReply instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}